﻿using Core.Framework;
using DotNetty.Codecs.Http;
using DotNetty.Framework.BaseModel;
using Newtonsoft.Json;
using System;
using System.Collections.Specialized;
using System.Text;

namespace iTool.Connector.ModelOfRequest
{
    public class FunctionHttpContext : AbstractHttpContext
    {
        public FunctionHttpContext()
        {
            this.ResponseHeader.Add(HttpHeaderNames.ContentType, "application/json; charset=utf-8");
            this.ResponseHeader.Add(HttpHeaderNames.AccessControlAllowOrigin, "*");
            this.ResponseEncoding = Encoding.UTF8;
            this.ResponseStatus = HttpResponseStatus.OK;
        }

        public NameValueCollection GetParameter()
        {
            return this.GetQueryString(this.GetParameterAsString());
        }

        public T GetParameter<T>()
        {
            string parameterString = this.GetParameterAsString();

            if (string.IsNullOrWhiteSpace(parameterString))
                return default(T);

            try
            {
                if (parameterString.StartsWith("{") & parameterString.EndsWith("}"))
                {
                    return JsonConvert.DeserializeObject<T>(parameterString);
                }
                else
                {
                    var query = this.GetQueryString(this.GetParameterAsString());
                    return JsonConvert.DeserializeObject<T>(query.TryToJson());
                }
            }
            catch
            {
                return default(T);
            }
        }

        /// <summary>
        /// 将查询字符串解析转换为名值集合.
        /// </summary>
        /// <param name="queryString"></param>
        /// <param name="encoding"></param>
        /// <param name="isEncoded"></param>
        /// <returns></returns>
        private NameValueCollection GetQueryString(string queryString)
        {
            NameValueCollection result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
            if (!string.IsNullOrEmpty(queryString))
            {
                int count = queryString.Length;
                for (int i = 0; i < count; i++)
                {
                    int startIndex = i;
                    int index = -1;
                    while (i < count)
                    {
                        char item = queryString[i];
                        if (item == '=')
                        {
                            if (index < 0)
                            {
                                index = i;
                            }
                        }
                        else if (item == '&')
                        {
                            break;
                        }
                        i++;
                    }
                    string key = null;
                    string value = null;
                    if (index >= 0)
                    {
                        key = queryString.Substring(startIndex, index - startIndex);
                        value = queryString.Substring(index + 1, (i - index) - 1);
                    }
                    else
                    {
                        key = queryString.Substring(startIndex, i - startIndex);
                    }

                    result[key] = value;

                    if ((i == (count - 1)) && (queryString[i] == '&'))
                    {
                        result[key] = string.Empty;
                    }
                }
            }
            return result;
        }

    }
}
